//
//  AppDelegate.m
//  Tour
//
//  Created by 张张烨 on 16/3/7.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "AppDelegate.h"
#import "PrefixHeader.pch"
#import "JPUSHService.h"
#import <iflyMSC/IFlySetting.h>
/**<  qq分享 */
#import <TencentOpenAPI/TencentOAuth.h>
@interface AppDelegate ()

@end
@interface WBBaseRequest ()
- (void)debugPrint;
@end

@interface WBBaseResponse ()
- (void)debugPrint;
@end
@implementation AppDelegate

@synthesize wbtoken;
@synthesize wbCurrentUserID;
@synthesize wbRefreshToken;




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    
    
    //初始化融云
    [[RCIM sharedRCIM] initWithAppKey:@"x4vkb1qpvdipk"];
    
  
    /**<  极光推送 */
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 0.8 ) {
        // 可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
    }else {
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    
    // Required
    //如需兼容旧版本的方式，请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化和同时使用pushConfig.plist文件声明appKey等配置内容。
    [JPUSHService setupWithOption:launchOptions appKey:@"0cb2065e50a6026f8c35d002" channel:nil apsForProduction:NO];
    
    
    
    //设置sdk的log等级，log保存在下面设置的工作路径中
    [IFlySetting setLogFile:LVL_ALL];
    
    //打开输出在console的log开关
    [IFlySetting showLogcat:YES];
    
    //输出版本号
    NSLog(@"IFlyMSC version:%@",[IFlySetting getVersion]);
    
    //设置sdk的工作路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    [IFlySetting setLogFilePath:cachePath];
    
    
    //创建语音配置,appid必须要传入，仅执行一次则可
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"56e6bb04"];
    
    //所有服务启动前，需要确保执行createUtility
#pragma message "'createUtility' should be call before any business using."
    [IFlySpeechUtility createUtility:initString];
    
    
    /**<微博分享*/
    
    //注册appkey
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:KAppKey];
    
    
   
    
    
    // 首页 home
    HomepageViewController *VC = [[HomepageViewController alloc]init];
    BaseNavigationController *navi = [[BaseNavigationController alloc]initWithRootViewController:VC];
    self.tabBar = [[UITabBarController alloc]init];
    
    //导航栏背景图片
    [navi.navigationBar setBackgroundImage:[UIImage imageNamed:@"999"] forBarMetrics:UIBarMetricsDefault];

     VC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:[[UIImage imageNamed:@"333"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:1000];
    
    // 目的地 site
    
    DestinationViewController *stVC = [[DestinationViewController alloc]init];

    BaseNavigationController *navi1 = [[BaseNavigationController alloc]initWithRootViewController:stVC];
//    stVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"目的地" image:[[UIImage imageNamed:@"96"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]tag:2000] ;
    // 社区 group
    TravelsViewController *GVC = [[TravelsViewController alloc]init];
    BaseNavigationController *navi2 = [[BaseNavigationController alloc]initWithRootViewController:GVC];
    [navi2.navigationBar setBackgroundImage:[UIImage imageNamed:@"999"] forBarMetrics:UIBarMetricsDefault];
//    navi2.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"999"]];
//    GVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"旅行" image:[[UIImage imageNamed:@"25"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:1100];
    // 我的 My
//    MyViewController *myVC = [[MyViewController alloc]init];
//    BaseNavigationController *navi3 = [[BaseNavigationController alloc]initWithRootViewController:myVC];
//    myVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[[UIImage imageNamed:@"69"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] tag:1220];
//    UINavigationController *navi1 = [[UINavigationController alloc]initWithRootViewController:stVC];
    //导航栏背景图片
 //导航栏背景图片
    [navi1.navigationBar setBackgroundImage:[UIImage imageNamed:@"999"] forBarMetrics:UIBarMetricsDefault];
    
    stVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"目的地" image:[[UIImage imageNamed:@"222"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]tag:2000] ;
    
    // 社区 group
//    TravelsViewController *GVC = [[TravelsViewController alloc]init];
//   UINavigationController *navi2 = [[UINavigationController alloc]initWithRootViewController:GVC];
    [navi2.navigationBar setBackgroundImage:[[UIImage imageNamed:@"999"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forBarMetrics:UIBarMetricsDefault];

   
 GVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"旅行" image:[[UIImage imageNamed:@"111"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:1100];
    
   
    
    // 我的 My
    MyViewController *myVC = [[MyViewController alloc]init];
    UINavigationController *navi3 = [[UINavigationController alloc]initWithRootViewController:myVC];
    myVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[[UIImage imageNamed:@"888"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] tag:1220];
    
    //导航栏背景图片
    [navi3.navigationBar setBackgroundImage:[UIImage imageNamed:@"999"] forBarMetrics:UIBarMetricsDefault];
    _tabBar.viewControllers = @[navi,navi1, navi2, navi3];

   
    
    
    //判断是不是第一次启动应用
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        NSLog(@"第一次启动");
        //        如果是第一次启动的话,使用UserGuideViewController (用户引导页面) 作为根视图
        GuidePageViewController *userGuideViewController = [[GuidePageViewController alloc] init];
        
        UINavigationController *ns = [[UINavigationController alloc] initWithRootViewController:userGuideViewController];
        
        self.window.rootViewController = ns;
        

    
    }
    else
    {
        NSLog(@"不是第一次启动");
        //如果不是第一次启动的话,使用LoginViewController作为根视图
        
       self.window.rootViewController = _tabBar;
    }


//
//    // 标签栏透明
//    _tabBar.tabBar.translucent = YES;
//    // 标签栏颜色
//    _tabBar.tabBar.barTintColor = [UIColor whiteColor];

    
    //设置文本颜色
    _tabBar.tabBar.tintColor = [UIColor redColor];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
      return YES;

    
    
    
    return YES;

}

/**<  qq*/
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    // 微信
    return [TencentOAuth HandleOpenURL:url];
    
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    // 微信
    return [TencentOAuth HandleOpenURL:url];
    
    }


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
    NSLog(@"%@",deviceToken);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // IOS 7 Support Required
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
